home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Reference Guide
/
C-C++ Interactive Reference Guide.iso
/
c_ref
/
csource3
/
184_01
/
runamd.doc
< prev
next >
Wrap
Text File
|
1979-12-31
|
12KB
|
212 lines
.HE HARDWARE FLOATING POINT ROUTINES FOR C/80-----------Ted Carnevale
Wheε ╔ talkeΣ witΦ Wal⌠ Bilofsk∙ oµ thσ Softwarσ Toolwork≤ ì
ove≥ ß yea≥ ago¼ hσ suggesteΣ tha⌠ ╔ senΣ thi≤ t∩ thσ ├ Usersº ì
Group¼ s∩ herσ i⌠ is« Additiona∞ detail≤ abou⌠ thσ desigε oµ ì
thesσ routines¼ anΣ possiblσ extension≤ t∩ othe≥ 8080/Z8░ ├ ì
implementations¼ arσ containeΣ iε m∙ articlσ iε thσ Nov/Deπ 198╡ ì
issuσ oµ Micro/System≤ Journa∞ (pp.46-54).
Mos⌠ oµ thσ codσ iε thesσ file≤ wa≤ writteε b∙ me¼ anΣ ╔ ì
releasσ i⌠ t∩ thσ publiπ domain¼ grantinτ permissioε fo≥ non-ì
profi⌠ use« Thσ smal∞ amoun⌠ oµ codσ iε thesσ file≤ tha⌠ come≤ ì
directl∙ froφ thσ C/8░ floating-poin⌠ librar∙ i≤ copyrigh⌠ 198│ ì
b∙ Walte≥ Bilofsky¼ anΣ useΣ b∙ permission.
Thesσ file≤ contaiε ß replacemen⌠ fo≥ par⌠ oµ thσ C/8░ ì
(Softwarσ Toolworks¼ versioε 3.░ fo≥ 8080⌐ floatinτ poin⌠ librar∙ ì
tha⌠ use≤ thσ AM─ 9511┴ o≥ it≤ Inte∞ twin¼ thσ 8231┴ (hencefortΦ ì
referreΣ t∩ a≤ thσ FPP⌐ t∩ speeΣ u≡ floatinτ poin⌠ matΦ b∙ morσ ì
thaε aε orde≥ oµ magnitude« I⌠ take≤ 23╕ second≤ fo≥ C/8░ t∩ ruε ì
Savage'≤ benchmarδ (Dr.Dobb'≤ Journa∞ vol.9┤ (Aug.1984⌐ pp.110-ì
114⌐ oε ß 6mH· 8085¼ bu⌠ ß 4mH· machinσ witΦ ß 2mH· FP╨ need≤ ì
onl∙ 1╢ second≤ usinτ thesσ routines«
MCHIP8░ b∙ Jame≤ Dicδ (CU╟ 162⌐ doe≤ mucΦ thσ samσ a≤ thσ ì
patcΦ describeΣ here¼ bu⌠ therσ arσ somσ importan⌠ differences« ì
MCHIP8░ illustrate≤ ho≈ t∩ modif∙ thσ performancσ oµ softwarσ ì
buil⌠ froφ ß librar∙ oµ linkablσ module≤ wheε al∞ tha⌠ i≤ knowε ì
abou⌠ theφ i≤ thσ name≤ oµ thei≥ entr∙ points¼ ho≈ the∙ pas≤ ì
parameter≤ anΣ results¼ anΣ wha⌠ function≤ the∙ accomplish« ì
Unfortunately¼ thσ differencσ betweeε thσ format≤ useΣ b∙ C/8░ ì
anΣ thσ FP╨ t∩ represen⌠ floatinτ poin⌠ value≤ complicate≤ thi≤ ì
approach« I⌠ force≤ thσ use≥ oµ MCHIP8░ t∩ abandoε initializaì
tioε oµ floats¼ anΣ require≤ specia∞ atof(⌐ anΣ ftoa(⌐ functions.
M∙ aiφ wa≤ t∩ squeezσ a≤ mucΦ speeΣ a≤ possiblσ ou⌠ oµ C/8░ ì
whilσ hidinτ unnecessar∙ detail≤ froφ thσ user« ╔ particularl∙ ì
didn'⌠ wan⌠ t∩ bσ bothereΣ witΦ anythinτ tha⌠ woulΣ restric⌠ ì
portabilit∙ oµ sourcσ code¼ sincσ ╔ collaboratσ witΦ othe≥ user≤ ì
oµ C/8░ wh∩ d∩ no⌠ havσ thσ benefi⌠ oµ aε FPP« Thσ mos⌠ direc⌠ ì
wa∙ t∩ d∩ thi≤ wa≤ t∩ rewritσ par⌠ oµ C/80'≤ floatinτ poin⌠ ì
library« Fortunately¼ Softwarσ Toolwork≤ provide≤ thσ sourcσ ì
code¼ s∩ thi≤ tasδ wa≤ ß relativel∙ straightforward.
ABOUT THE PATCH
Thi≤ patcΦ fo≥ C/80'≤ floatinτ poin⌠ librar∙ handle≤ al∞ oµ ì
thσ dirt∙ worδ relateΣ t∩ conversioε betweeε thσ differen⌠ ì
floatinτ poin⌠ representation≤ useΣ b∙ C/8░ anΣ thσ FP╨ (seσ ì
note≤ beforσ labe∞ C2AMD║ iε FL3.MAC)« C/8░ anΣ thσ FP╨ botΦ usσ ì
ß 2┤ bi⌠ two'≤ complemen⌠ mantissß normalizeΣ t∩ ß valuσ betweeε ì
0.╡ anΣ 1« However¼ C/8░ assume≤ tha⌠ thσ higΦ bi⌠ oµ thσ ì
mantissß i≤ ▒ (reasonablσ sincσ it'≤ normalized)¼ anΣ use≤ thσ ì
24tΦ bi⌠ fo≥ mantissß sign¼ s∩ i⌠ caε represen⌠ number≤ froφ 2^-ì
12╕ t∩ 2^127« Thσ 9511┴ o≥ 8231A¼ whicΦ retain≤ thσ higΦ bi⌠ oµ ì
thσ mantissa¼ ha≤ t∩ pu⌠ thσ mantissß sigε bi⌠ iε thσ exponen⌠ ì
byte¼ anΣ caε handlσ onl∙ 2^-6┤ t∩ 2^+63.
Thσ conversioε routine≤ arσ a⌠ label≤ C2AMD║ anΣ AMD2C║ iε ì
FL3.MAC« Thesσ routine≤ arσ alway≤ interposeΣ betweeε thσ res⌠ ì
oµ C/8░ anΣ thσ FPP¼ s∩ thσ use≥ caε emplo∙ C/80'≤ floatinτ poin⌠ ìèinitializatioε anΣ thσ origina∞ atof(⌐ anΣ ftoa(⌐ function≤ ì
withou⌠ concerε fo≥ thσ forma⌠ difference.
╔ firs⌠ wrotσ thesσ routine≤ a≤ function≤ anΣ testeΣ theφ ì
witΦ fpπ (filσ FPC.C)¼ ß prograφ tha⌠ print≤ thσ bi⌠ pattern≤ fo≥ ì
variou≤ floatinτ poin⌠ value≤ beforσ anΣ afte≥ conversion« Thi≤ ì
prograφ ma∙ bσ helpfu∞ fo≥ user≤ witΦ othe≥ ├ implementation≤ ì
whosσ inne≥ working≤ arσ no⌠ detaileΣ iε thσ manual« Filσ ì
C80DEF.╚ contain≤ definition≤ tha⌠ ╔ finΣ helpfu∞ wheε usinτ ì
C/80.
Somσ oµ thσ routine≤ iε thi≤ patcΦ arσ concerneΣ witΦ ì
passinτ argument≤ froφ C/8░ t∩ thσ FP╨ anΣ returninτ result≤ t∩ ì
C/80« Thσ brieµ descriptioε oµ paramete≥ anΣ resul⌠ passinτ iε ì
thσ C/8░ manua∞ i≤ accompanieΣ b∙ ß suggestioε tha⌠ interesteΣ ì
user≤ examinσ thσ codσ produceΣ b∙ thσ compile≥ t∩ figurσ i⌠ ou⌠ ì
fo≥ themselves« Thσ sourcσ codσ iε file≤ LGFLTLIB.AS═ anΣ ì
FLOATLIB.AS═ tha⌠ comσ witΦ C/8░ wa≤ ver∙ helpfu∞ fo≥ decipherinτ ì
wha⌠ happen≤ t∩ argument≤ anΣ functioε results.
Thσ typica∞ floatinτ poin⌠ operatioε require≤ morσ thaε 16╖ ì
microsecond≤ (usec⌐ jus⌠ t∩ conver⌠ format≤ anΣ pas≤ datß bacδ ì
anΣ fortΦ t∩ thσ FP╨ oε ß 4mH· machinσ (8080¼ 808╡ o≥ Z80)« Thi≤ ì
i≤ slo≈ enougΦ tha⌠ i⌠ doesn'⌠ seeφ wortΦ thσ effor⌠ t∩ replacσ ì
thσ floating-poin⌠ additioε anΣ subtractioε routines« ╔ als∩ ì
didn'⌠ bothe≥ replacinτ floatinτ poin⌠ comparison¼ o≥ int-to-ì
floa⌠ conversioε routines« However¼ division¼ multiplication¼ ì
anΣ thσ othe≥ function≤ arσ mucΦ faste≥ witΦ thσ hardwarσ matΦ ì
chip.
Test≤ indicateΣ tha⌠ i⌠ woulΣ bσ beneficia∞ t∩ replacσ thσ ì
softwarσ multiplicatioε anΣ divisioε routine≤ fo≥ long≤ witΦ ì
hardwarσ operations« C/8░ anΣ thσ FP╨ usσ thσ samσ four-bytσ ì
forma⌠ fo≥ longs¼ s∩ thi≤ shoulΣ bσ easy« An∙ volunteer≤ t∩ d∩ ì
this?
Beforσ replacinτ C/80'≤ floatinτ poin⌠ operation≤ witΦ FP╨ ì
routines¼ ╔ wrotσ ß se⌠ oµ function≤ tha⌠ usσ thσ FP╨ t∩ ì
duplicatσ C/80'≤ floatinτ poin⌠ operations¼ anΣ assembleΣ theφ ì
witΦ M80« ╔ testeΣ theφ witΦ ß prograφ tha⌠ steppeΣ througΦ ß ì
serie≤ oµ arguments¼ feedinτ theφ t∩ C80'≤ origina∞ floatinτ ì
poin⌠ librar∙ anΣ t∩ m∙ specia∞ FP╨ routines¼ anΣ compareΣ thσ ì
result≤ t∩ bσ surσ the∙ gavσ thσ samσ answer≤ a≤ thei≥ softwarσ ì
counterparts« WhicΦ the∙ did.
╔ dissecteΣ FLIBRARY.RE╠ witΦ LI┬ usinτ thσ /╠ optioε t∩ ì
determinσ tha⌠ thσ modulσ tha⌠ containeΣ thσ floatinτ poin⌠ ì
multiplicatioε anΣ divisioε routine≤ wa≤ calleΣ FLTLIB« Thσ ì
sourcσ codσ filσ FLOATLIB.AS═ contain≤ thσ sourcσ fo≥ severa∞ ì
modules¼ includinτ FLTLIB« FLTLI┬ end≤ witΦ ß RE╘ jus⌠ beforσ ì
thσ labe∞ f_stak:.
HOW TO INSTALL THE PATCH
Thσ file≤ FL1.MAC¼ FL2.MAC¼ anΣ FL3.MA├ arσ replacement≤ fo≥ ì
portion≤ oµ thσ FLOATLIB.AS═ filσ provideΣ witΦ C/80« Thesσ werσ ì
writteε fo≥ Microsoft'≤ M80¼ bu⌠ the∙ caε bσ altereΣ easil∙ fo≥ ì
usσ witΦ Digita∞ Research'≤ RMA├ o≥ thσ assemble≥ provideΣ witΦ ì
C/80.
Usinτ you≥ favoritσ editor¼ extrac⌠ thσ FLTLI┬ codσ froφ ìèFLOATLIB« AdΣ thσ lis⌠ oµ ENTRYs¼ EXTRNs¼ DSEG≤ etc« containeΣ ì
iε FL1.MAC« Modif∙ thσ fou≥ worΣ tablσ a⌠ Ftab║ a≤ indicateΣ b∙ ì
FL2.MA├ (commen⌠ ou⌠ thσ thirΣ anΣ fourtΦ DWs)« Theε cu⌠ ou⌠ thσ ì
codσ froφ fmult3║ t∩ jus⌠ beforσ pophrt║ anΣ replacσ i⌠ witΦ thσ ì
content≤ oµ FL3.MAC« Bσ surσ t∩ changσ thσ i/∩ por⌠ addresse≤ t∩ ì
sui⌠ you≥ particula≥ hardware!
╔ havσ no⌠ includeΣ thσ absolutσ valuσ o≥ polynomia∞ ì
function≤ (fabs(⌐ anΣ F_poly()⌐ iε thi≤ patch¼ althougΦ the∙ arσ ì
par⌠ oµ thσ original MATHLIB« Iµ yo⌡ wan⌠ t∩ includσ thesσ iε you≥ ì
versioε oµ FLTL┬ (yes¼ ╔ nameΣ thσ patcheΣ modulσ FLTL┬ t∩ ì
distinguisΦ i⌠ froφ thσ origina∞ FLTLIB)¼ usσ C/8░ t∩ generatσ ì
thσ appropriatσ assemble≥ codσ froφ thσ ├ sourcσ anΣ patcΦ thσ ì
assembl∙ codσ int∩ you≥ FLTL┬ beforσ assembly« Iµ yo⌡ arσ reall∙ ì
ambitious¼ yo⌡ coulΣ speeΣ u≡ Fpoly(⌐ eveε morσ b∙ usinτ ß ì
modifieΣ "synthetiπ divisionó algorithφ tha⌠ leave≤ intermediatσ ì
result≤ iε thσ FPP'≤ stack« B∙ eliminatinτ repeateΣ unnecessar∙ ì
conversion≤ oµ intermediatσ result≤ t∩ anΣ froφ C/80'≤ floa⌠ ì
format¼ thi≤ shoulΣ acceleratσ Fpoly(⌐ considerably.
Whateve≥ you≥ choice¼ thσ nex⌠ ste≡ i≤ t∩ namσ thσ resultinτ ì
filσ NUFLIB.MA├ anΣ assemblσ i⌠ witΦ M8░ t∩ producσ NUFLIB.REL« ì
No≈ usσ LI┬ t∩ sni≡ thσ FLTLI┬ modulσ ou⌠ oµ FLIBRARY.RE╠ tha⌠ ì
camσ witΦ you≥ cop∙ oµ C/8░ anΣ replacσ i⌠ witΦ NUFLIB« Thσ ì
followinτ SUBMI╘ filσ shoulΣ d∩ thσ job:
;start of runlib.sub
XSUB
LIB
NEWLIB=FLIBRARY<..FLTLIB-1>
NUFLIB,FLIBRARY<FLTLIB+1..>
/E
;end of runlib.sub
Thσ resul⌠ shoulΣ bσ NEWLIB.REL¼ whicΦ wil∞ usσ thσ AM─ 9511┴ o≥ ì
Inte∞ 8231┴ fo≥ floatinτ poin⌠ operations« Feedinτ NEWLI┬ ì
insteaΣ oµ FLIBRAR┘ t∩ you≥ linkinτ loade≥ wil∞ producσ CO═ file≤ ì
that use the FPP for floating point math.
Don'⌠ forge⌠ tha⌠ NEWLI┬ contain≤ thσ squarσ roo⌠ anΣ tranì
scendenta∞ function≤ tha⌠ arσ par⌠ oµ thσ olΣ MATHLIB« Includinτ ì
botΦ NEWLI┬ anΣ MATHLI┬ iε command≤ t∩ you≥ linke≥ ma∙ producσ aε ì
erro≥ messagσ indicatinτ duplicatioε oµ thesσ routines«
Iµ yo⌡ havσ alread∙ useΣ LI┬ t∩ builΣ ß singlσ librar∙ ou⌠ ì
oµ al∞ oµ thσ RE╠ file≤ tha⌠ yo⌡ use¼ yo⌡ wil∞ havσ troublσ ì
extractinτ MATHLI┬ froφ you≥ library« LI┬ caε incorporatσ ì
module≤ witΦ ╖ characte≥ name≤ int∩ ß library¼ bu⌠ sincσ i⌠ wil∞ ì
onl∙ accep⌠ name≤ witΦ ß maximuφ oµ ╢ character≤ wheε yo⌡ tr∙ t∩ ì
acces≤ individua∞ module≤ iε ß library¼ i⌠ won'⌠ bσ ablσ t∩ finΣ ì
MATHLIB« Iµ thi≤ happens¼ yo⌡ havσ tw∩ choices║ builΣ ß ne≈ ì
librar∙ froφ scratch╗ o≥ usσ LI┬ witΦ it≤ /╠ optioε t∩ identif∙ ì
thσ namσ oµ thσ las⌠ modulσ tha⌠ precede≤ MATHLI(B⌐ anΣ thσ namσ ì
oµ thσ firs⌠ modulσ tha⌠ follow≤ it« If¼ fo≥ thσ sakσ oµ ì
argument¼ thesσ module≤ werσ calleΣ PRE╓ anΣ NEXT¼ anΣ you≥ biτ ├ ì
librar∙ wa≤ nameΣ CLIB¼ theε
LIB XCLIB=CLIB<..PREV>,CLIB<NEXT..>/E
èwill extract MATHLIB from CLIB, producing library XCLIB.
ERROR-DETECTION AND HANDLING
Thσ patcheΣ FLTL┬ ha≤ specia∞ error-detectioε anΣ handlinτ ì
routines« Tw∩ type≤ oµ error≤ ma∙ occur« Aε attemp⌠ t∩ pas≤ aε ì
argumen⌠ tha⌠ i≤ to∩ smal∞ o≥ to∩ biτ fo≥ thσ FP╨ (outsidσ thσ ì
rangσ 2^-6┤ t∩ 2^63⌐ produce≤ ß softwarσ error« Thσ FP╨ itselµ ì
produce≤ ß hardwarσ erro≥ iµ i⌠ encounter≤ arithmetiπ overflow¼ ì
underflow¼ dividσ b∙ zero¼ takinτ thσ loτ o≥ squarσ roo⌠ oµ ß ì
negativσ number¼ o≥ aε argumen⌠ to∩ biτ fo≥ thσ exponentia∞ o≥ ì
inversσ sinσ o≥ cosinσ functions« A≤ FLTL┬ i≤ presentl∙ written¼ ì
an∙ onσ oµ thesσ error≤ result≤ iε aε erro≥ messagσ anΣ thσ codσ ì
wil∞ exi⌠ t∩ thσ operatinτ system.
A⌠ time≤ i⌠ ma∙ bσ ß bette≥ strateg∙ t∩ handlσ certaiε ì
hardwarσ errors¼ sucΦ a≤ underflow¼ b∙ returninτ ß zer∩ o≥ somσ ì
othe≥ valuσ insteaΣ oµ abortinτ thσ program« Thereforσ ╔ ì
includeΣ ß functioε fpmask(⌐ tha⌠ caε bσ useΣ t∩ se⌠ thσ erro≥ ì
detectioε masδ unde≥ prograφ control« Thσ defaul⌠ conditioε i≤ ì
t∩ tes⌠ for¼ anΣ crasΦ iε casσ of¼ an∙ hardwarσ error« Iµ an∙ oµ ì
thσ FP╨ erro≥ test≤ i≤ disabled¼ i⌠ i≤ u≡ t∩ thσ programme≥ t∩ ì
tes⌠ fo≥ anΣ handlσ sucΦ error≤ iε ß meaningfu∞ way« Thi≤ migh⌠ ì
bσ donσ witΦ ß ├ routinσ tha⌠ check≤ thσ statu≤ registe≥ ì
immediatel∙ afte≥ an∙ suspec⌠ operation¼ branchinτ t∩ aε error-ì
handlinτ routinσ iµ aε erro≥ occurs.
CONCLUSION
Thσ fina∞ resul⌠ oµ Savage'≤ benchmarδ usinτ thσ FP╨ i≤ 232╖ ì
(roundeΣ ofµ t∩ thσ neares⌠ integer--erro≥ 2E+2)« Thi≤ i≤ ì
comparablσ t∩ singlσ precisioε FORTRAN¼ slightl∙ bette≥ accurac∙ ì
(ß dubiou≤ terφ here⌐ thaε witΦ softwarσ arithmetic¼ anΣ jus⌠ a≤ ì
gooΣ (o≥ bad⌐ a≤ an∙ othe≥ Am951▒ o≥ Inte∞ 8231┴ FP╨ iε thσ list« ì
I⌠ i≤ no⌠ sufficien⌠ fo≥ ill-conditioneΣ matri° equations¼ bu⌠ i⌠ ì
i≤ morσ thaε adequatσ fo≥ les≤ demandinτ applications¼ sucΦ a≤ ì
averaginτ anΣ displa∙ transformation≤ oµ datß followinτ A/─ ì
conversion« However¼ thσ speeΣ oµ operatioε i≤ faste≥ thaε an∙ ì
othe≥ ╕ bi⌠ machinσ runninτ a⌠ 4mHz¼ anΣ abou⌠ 1╡ time≤ faste≥ ì
thaε C80'≤ softwarσ floatinτ poin⌠ library«
Keep on crunching!
Ted Carnevale
Neurology Dept., SUNY
HSC T12 Rm020
Stony Brook, N.Y. 11794
12/27/85